package com.likuicat.picture_ddd.application.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.likuicat.picture_ddd.domain.user.service.UserDomainService;
import com.likuicat.picture_ddd.infrastructure.common.DeleteRequest;
import com.likuicat.picture_ddd.infrastructure.exception.BusinessException;
import com.likuicat.picture_ddd.infrastructure.exception.ErrorCode;
import com.likuicat.picture_ddd.infrastructure.exception.ThrowUtils;
import com.likuicat.picture_ddd.interfaces.dto.user.UserLoginRequest;
import com.likuicat.picture_ddd.interfaces.dto.user.UserQueryRequest;
import com.likuicat.picture_ddd.domain.user.entity.User;
import com.likuicat.picture_ddd.interfaces.dto.user.UserRegisterRequest;
import com.likuicat.picture_ddd.interfaces.vo.user.LoginUserVO;
import com.likuicat.picture_ddd.interfaces.vo.user.UserVO;
import com.likuicat.picture_ddd.application.service.UserApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
* @author fc3379757631
* @description 针对表【user(用户)】的数据库操作Service实现
* @createDate 2025-05-04 15:13:06
*/
@Service
@Slf4j
public class UserApplicationServiceImpl implements UserApplicationService {

    @Resource
    private UserDomainService userDomainService;
    /**
     * 用户注册
     * @param userRegisterRequest
     * @return
     */
    @Override
    public long userRegister(UserRegisterRequest userRegisterRequest) {
        String userAccount = userRegisterRequest.getUserAccount();
        String password = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        // 校验
        User.validUserRegister(userAccount, password, checkPassword);
        // 执行
        return userDomainService.userRegister(userAccount, password, checkPassword);
    }

    @Override
    public LoginUserVO userLogin(UserLoginRequest userLoginRequest, HttpServletRequest request) {
        String userAccount = userLoginRequest.getUserAccount();
        String password = userLoginRequest.getUserPassword();
        // 校验
        User.validUserLogin(userAccount, password);
        // 执行
        return userDomainService.userLogin(userAccount, password, request);
    }

    /**
     * 获取当前登录用户
     * @param request
     * @return
     */
    @Override
    public User getLoginUser(HttpServletRequest request) {
        return userDomainService.getLoginUser(request);
    }

    /**
     * 用户注销
     * @param request
     * @return
     */
    @Override
    public boolean userLogout(HttpServletRequest request) {
        return userDomainService.userLogout(request);
    }

    /**
     * 加密密码
     * @param password
     * @return
     */
    @Override
    public String getEncryptPassword(String password) {
        return userDomainService.getEncryptPassword(password);
    }

    /**
     * 返回脱敏用户LoginUserVO
     * @param user
     * @return
     */
    @Override
    public LoginUserVO getUserToLoginUserVO(User user){
        return userDomainService.getUserToLoginUserVO(user);
    }

    /**
     * 返回脱敏用户UserVO
     * @param user
     * @return
     */
    @Override
    public UserVO getUserToUserVO(User user) {
        return userDomainService.getUserToUserVO(user);
    }

    /**
     * 返回脱敏UserVO集合类
     * @param userList
     * @return
     */
    @Override
    public List<UserVO> getUserVOList(List<User> userList) {
        return userDomainService.getUserVOList(userList);
    }

    /**
     * 将查询请求转化为QueryWrapper
     * @param userQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<User> getUserQueryWrapper(UserQueryRequest userQueryRequest) {
        return userDomainService.getUserQueryWrapper(userQueryRequest);
    }


    @Override
    public User getUserById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        User user = userDomainService.getById(id);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
        return user;
    }

    @Override
    public boolean deleteUser(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return userDomainService.removeById(deleteRequest.getId());
    }

    @Override
    public Page<UserVO> listUserVOByPage(UserQueryRequest userQueryRequest) {
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        long current = userQueryRequest.getCurrent();
        long size = userQueryRequest.getPageSize();
        Page<User> userPage = userDomainService.page(new Page<>(current, size),
                userDomainService.getUserQueryWrapper(userQueryRequest));
        Page<UserVO> userVOPage = new Page<>(current, size, userPage.getTotal());
        List<UserVO> userVO = userDomainService.getUserVOList(userPage.getRecords());
        userVOPage.setRecords(userVO);
        return userVOPage;
    }

    @Override
    public List<User> listByIds(Set<Long> userIdSet) {
        return userDomainService.listByIds(userIdSet);
    }

    @Override
    public void updateUser(User user) {
        boolean updated = userDomainService.updateById(user);
        ThrowUtils.throwIf(!updated, ErrorCode.OPERATION_ERROR,"更新失败");
    }

    @Override
    public Long saveUser(User user) {
        //默认密码
        final String DEFAULT_PASSWORD = "12345678";
        //加密
        String encryptPassword = this.getEncryptPassword(DEFAULT_PASSWORD);
        user.setUserPassword(encryptPassword);
        boolean save = userDomainService.save(user);
        ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR,"添加失败");
        return user.getId();
    }


}




